package com.happyclub.lbj.service.impl;

import com.happyclub.lbj.service.IActivityCommontReply;
import com.happyclub.lbjdao.ActivityCommontReply;
import com.happyclub.paul.dao.UserRepostitory;
import com.happyclub.pojo.TbActivityCommontReply;
import com.happyclub.pojo.TbUser;
import com.happyclub.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

import java.util.List;

import static com.happyclub.errormsg.ErrorMsgProvider.SYSTEM_BUSY;

/**
 * @author 高欢
 * @description TODO
 * @date 2019/4/8
 */
@Service
public class ActivityCommontReplyImpl implements IActivityCommontReply {
    @Autowired
    ActivityCommontReply IactivityCommontReply;

    @Autowired
    private UserRepostitory userRepostitory;
    @Transactional
    @Override
    public JsonResult saveActivityCommontReply(TbActivityCommontReply activityCommontReply) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        TbActivityCommontReply save = IactivityCommontReply.save(activityCommontReply);
        if (save==null){
            jsonResult.setErrorCode("500");
            jsonResult.setMsg(SYSTEM_BUSY);
            return jsonResult;
        }
        jsonResult.setResult(save);
        return jsonResult;
    }

    @Transactional
    @Override
    public JsonResult delActivityCommontReply(String id) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        IactivityCommontReply.deleteById(id);
        return jsonResult;
    }

    @Override
    public JsonResult findCommontReplyByCommontId(String id) {
        JsonResult<Object> jsonResult = new JsonResult<>();
        List<TbActivityCommontReply> replies = IactivityCommontReply.findTbActivityCommontRepliesByCommontIdOrderByCreateTime(id);
        if (replies.size()==0){
            jsonResult.setMsg("亲,目前该评论还没有回复...");
            jsonResult.setErrorCode("500");
            return jsonResult;
        }
        for (TbActivityCommontReply tc:replies){
            TbUser tbUser = userRepostitory.queryUserById(tc.getReplyUserId());
            TbUser tbUser1 = userRepostitory.queryUserById(tc.getUserId());
            tc.setReplyNickName(tbUser.getNickName());
            tc.setNickName(tbUser1.getNickName());
        }
        jsonResult.setResult(replies);
        return jsonResult;
    }
}
